package com.learn.netty.nio;

import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.UUID;

/**
 *
 * nio 客户端
 */
@Slf4j
public class NIOClient {


    /**
     * 客户端
     * @param args
     */
    public static void main(String[] args) throws Exception{

        // 1.获取一个客户端通道
        SocketChannel socketChannel = SocketChannel.open();

        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",6666);

        // 设置通道为非阻塞方式
        socketChannel.configureBlocking(false);


        // 连接服务器
        if (!socketChannel.connect(inetSocketAddress)) {
            while (!socketChannel.finishConnect()) {
                log.info("因为连接需要时间，客户端不会阻塞，可以做其他工作...");
            }
        }

        String str = "客户端发送数据：" + UUID.randomUUID();

        ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());
        socketChannel.write(byteBuffer);
        System.in.read();
    }
}
